<?php 
require_once 'core/init.php';	
	
	if (Input::exists()) {
		if (Token::check(Input::get('token'))) {

			//echo "this has been run </br>";
	
			$validate = new Validation();
			$validation = $validate->check($_POST, array(
			
				'username' => array(
					'required' => true ),
				'password' => array(
					'required' => true )
			
			));

			if ($validation->passed()) {
				echo "passed<br/>";
				$user = new User();
				$remember = (Input::get('remember') === 'on') ? true : false;
				$login = $user->login(Input::get('username'), Input::get('password'), $remember);

				if ($login) {
					Redirect::to('index.php');
				}else {
					echo "Incorrect Password";
				}
			}else {
				foreach ($validation->errors() as $error) {
					echo $error, '<br/>';
				}
			}

		}
	}

?>

<form action="" method="post">

	<div class="field">
		<label for="username">Username</label>
		<input type="text" name="username" id="username" value="" autocomplete="off">
	</div>

	<div class="field">
		<label for="password">Password</label>
		<input type="password" name="password" id="password" value="" autocomplete="off">
	</div>

	<div class="field">
		<label for="remember">Remember</label>
		<input type="checkbox" name="remember" id="remember"/>
	</div>

	<div class="field">
		<input type="submit" value="Login">
	</div>

	<div class="field">
		<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
	</div>
</form>